<template>
  <div class="app-container" v-loading="loading.getPageList">
    <div>
      <el-button type="primary" size="small" @click="create">新增</el-button>
      <el-button type="primary" size="small" @click="getPageList"
        >刷新</el-button
      >
    </div>
    <br />

    <el-table :data="tableData" border style="width: 100%" highlight-current-row>
      <el-table-column type="index" label="序号" width="60"> </el-table-column>
      <el-table-column prop="name" label="标题"> </el-table-column>
      <!-- <el-table-column prop="formDesignTitle" label="表单" width="200">
      </el-table-column> -->
      <el-table-column prop="workflowName" label="流程名称" width="250">
        <template slot-scope="scope">
          <span>{{ scope.row.workflowId.workflowName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.workTaskStatus == 0">待发起</span>
          <span v-if="scope.row.workTaskStatus == 1">审批中</span>
          <span v-if="scope.row.workTaskStatus == 2">已审批</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="creationTime"
        :formatter="dateFormat"
        label="创建时间"
        width="160"
      >
      </el-table-column>
      <el-table-column
        prop="modifiedTime"
        :formatter="dateFormat"
        label="更新时间"
        width="160"
      >
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.workTaskStatus != 0"
            @click="$refs.viewForm.show(scope.row.id)"
            type="text"
            size="small"
            >查看</el-button
          >
          <el-button
            v-if="scope.row.workTaskStatus == 0"
            @click="startWorkTask(scope.row.id)"
            type="text"
            size="small"
            >发起审批</el-button
          >
          <el-button
            v-if="scope.row.workTaskStatus == 0"
            @click="$refs.editForm.show(scope.row.id)"
            type="text"
            size="small"
            >编辑</el-button
          >
          <el-button
            v-if="scope.row.workTaskStatus == 0"
            @click="remove(scope.row.id)"
            type="text"
            size="small"
            >删除</el-button
          >
          <el-button
            @click="
              $refs.workflowDesignForm.show({
                id: scope.row.workflowId.id,
                version: scope.row.workflowId.versionNo,
                taskId:scope.row.id
              })
            "
            type="text"
            size="small"
            >流程图</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <br />
    <el-pagination
      background
      layout="sizes,prev, pager, next"
      :total="page.total"
      :page-size="page.maxResultCount"
      :current-page="page.currentPage"
      @current-change="(current) => paginationChange(null, current)"
      @size-change="(size) => paginationChange(size)"
    >
    </el-pagination>

    <viewForm ref="viewForm"></viewForm>
    <createForm ref="createForm" @ok-click="getPageList"></createForm>
    <editForm ref="editForm"></editForm>

    <workflowDesignForm ref="workflowDesignForm"></workflowDesignForm>
  </div>
</template>

<script>
import createForm from "./createForm";
import TableIndex from "@/layout/mixin/TableIndex";
import viewForm from "./../formInstance/viewForm";
import editForm from "./editForm";

import workflowDesignForm from "@/views/workflows/workflow/viewWorkflowForm.vue";
export default {
  components: { createForm, viewForm, editForm, workflowDesignForm },
  mixins: [TableIndex],
  data() {
    return {
      moduleName: "workflow",
      selections: [],
    };
  },
  mounted() {},
  methods: {
    getPageList() {
      this.loading.getPageList = true;
      this.page.skipCount =
        (this.page.currentPage - 1) * this.page.maxResultCount;
      this.$store
        .dispatch(this.moduleName + "/getWorkTasksOfCreator", this.page)
        .then((res) => {
          this.tableData = res.items;
          this.page.total = res.total;
          this.loading.getPageList = false;
        });
    },
    getDesignName(designId) {
      var designs = this.selections.filter((s) => s.id == designId);
      return designs && designs.length > 0 ? designs[0].title : "";
    },
    remove(id) {
      this.$confirm("确认删除该记录？, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$store
            .dispatch(this.moduleName + "/deleteWorkTask", {
              id: id,
            })
            .then((res) => {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.getPageList();
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    startWorkTask(workTaskId) {
      this.$confirm("确认发起审批？, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.loading.getPageList = true;
          const data = {
            workTaskId: workTaskId,
          };
          this.$store
            .dispatch("workflow/startWorkTask", data)
            .then((res) => {
              this.loading.getPageList = false;
              this.getPageList();
            })
            .catch((e) => {
              this.loading.getPageList = false;
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style></style>
